package com.coin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coin.domain.Market;
import com.coin.dto.MarketDto;
import com.coin.model.R;
import com.coin.service.MarketService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @Auther: 李 力
 * @Date: 2025/6/20
 * @Description: com.coin.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("/markets")
@Api(tags = "交易市场的控制器")
public class MarketController {

    @Autowired
    private MarketService marketService;

    /**
     * @Description: 交易市场的分页查询
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return: R
     */
    @GetMapping
    @ApiOperation(value = "交易市场的分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数")
    })
    @PreAuthorize("hasAuthority('trade_market_query')")
    public R<Page<Market>> findByPage(@ApiIgnore Page<Market> page, Long tradeAreaId, Byte status) {
        Page<Market> pageData = marketService.findByPage(page, tradeAreaId, status);
        return R.ok(pageData);
    }

    /**
     * @Description: 启用/禁用交易市场
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return: R
     */
    @PutMapping("/setStatus")
    @ApiOperation(value = "启用/禁用交易市场")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "market", value = "market的json数据")
    })
    @PreAuthorize("hasAuthority('trade_market_update')")
    public R setStatus(@RequestBody Market market) {
        boolean isOk = marketService.setStatus(market);
        return isOk ? R.ok() : R.fail();
    }

    /**
     * @Description: 新增一个市场
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return: R
     */
    @PostMapping
    @ApiOperation(value = "新增一个市场")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "market", value = "marketjson")
    })
    @PreAuthorize("hasAuthority('trade_market_create')")
    public R save(@RequestBody Market market) {
        boolean isOk = marketService.saveMarket(market);
        return isOk ? R.ok() : R.fail();
    }

    /**
     * @Description: 修改一个市场
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return: R
     */
    @PutMapping
    @ApiOperation(value = "修改一个市场")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "market", value = "marketjson")
    })
    @PreAuthorize("hasAuthority('trade_market_update')")
    public R update(@RequestBody Market market) {
        boolean isOk = marketService.updateMarket(market);
        return isOk ? R.ok() : R.fail();
    }

    /**
     * @Description: 查询所有的交易市场
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return: R
     */
    @GetMapping("/all")
    @ApiOperation(value = "查询所有的交易市场")
    public R<List<Market>> listMarks() {
        List<Market> markets = marketService.listMarket();
        return R.ok(markets);
    }

    /**
     * @Description: 远程调用
     * @Author: 李 力
     * @Date: 2025/6/23
     * @Param:
     * @return: R
     */
    @GetMapping("/getMarket")
    MarketDto findByCoinId(@RequestParam("buyCoinId") Long buyCoinId, @RequestParam("sellCoinId") Long sellCoinId) {
        return marketService.findByCoinId(buyCoinId, sellCoinId);
    }
}
